<?php
class ReportController extends Zend_Controller_Action {
	// Mảng tham số nhận được ở mỗi Action:
	protected $_arrParam;
	private $USER_ID = 0;
	private $URI = "/";
	private $URL_ACC_LOGIN = "/account/login/";
	private $DEFAULT_PROFILE_PICTURE = "default/img/no_image.jpg";
	private $ALERT_TYPE_ERROR = "alert-error";
	private $ALERT_TYPE_SUCCESS = "alert-success";
	public function init() {
		$this->_arrParam = $this->_request->getParams ();
		// Truyền ra view
		$this->view->arrParam = $this->_arrParam;
		
		// Xu li cho phan header
		$this->URI = $this->getRequest ()->getScheme () . "://" . $this->getRequest ()->getHttpHost () . $this->getRequest ()->getRequestUri ();
		$mAccount = new Default_Model_Account ();
		if (! $mAccount->hasIdentity ()) {
			$this->_redirect ( $this->URL_ACC_LOGIN . '?return_url=' . $this->URI );
		} else {
			// get user info
			$this->USER_ID = $mAccount->getUserId ();
			
			$this->view->fullname = $mAccount->getFullName ();
			$this->view->return_url = $this->URI;
			$this->view->userid = $mAccount->getUserId ();
			$strAvatar = $mAccount->getProfilePicture ();
			if ($strAvatar != "") {
				$this->view->my_avatar = $mAccount->getProfilePicture ();
			} else {
				$this->view->my_avatar = $this->DEFAULT_PROFILE_PICTURE;
			}
			// get report
			$mRep = new Default_Model_Report();
			$arrRep = $mRep->listAllReport();
			$this->view->arrRep = $arrRep;
		}
		// End xu li cho phan header
	}
	public function indexAction() {
		$mReport = new Default_Model_Report ();
		$mCmt = new Default_Model_Comment ();
		$mUser = new Default_Model_User ();
		$mLoc = new Default_Model_Location ();
		$mPhoto = new Default_Model_Photo();
		
		if ($this->_request->isGet ()) {
			if (isset ( $_GET ['delete'] ) && $_GET ['delete'] == 1) {
				if (isset ( $_GET ['report_id'] )) {
					// delete report
					if ($mReport->deleteReportById ( $_GET ['report_id'] )) {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_SUCCESS;
						$this->view->alert_message = 'Xoá báo cáo sai phạm thành công!';
					} else {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_ERROR;
						$this->view->alert_message = '<strong>Error!</strong> ' . $mReport->getError ();
					}
				} else {
					$this->view->display_alert = 1;
					$this->view->alert_type = "";
					$this->view->alert_message = '<strong>Warning!</strong> Thiếu thông tin để xoá .';
				}
			} else if (isset ( $_GET ['approve'] ) && $_GET ['approve'] == 1) {
				if (isset ( $_GET ['report_id'] ) && isset($_GET['cmt_id'])) {
					// delete comment
					$mProcess = new Default_Model_YourTourProcess ();
					$strMessReturn = $mProcess->deleteComment($_GET['cmt_id']);
					if ($strMessReturn == "") {
						// delete report
						if ($mReport->deleteReportById ( $_GET ['report_id'] )) {
							$this->view->display_alert = 1;
							$this->view->alert_type = $this->ALERT_TYPE_SUCCESS;
							$this->view->alert_message = 'Xác nhận báo cái sai phạm thành công. Bình luận sai phạm đã được xoá.';
						} else {
							$this->view->display_alert = 1;
							$this->view->alert_type = $this->ALERT_TYPE_ERROR;
							$this->view->alert_message = '<strong>Error!</strong> ' . $mReport->getError ();
						}
					} else {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_ERROR;
						$this->view->alert_message = '<strong>Error!</strong> ' . $strMessReturn;
					}
				} else {
					$this->view->display_alert = 1;
					$this->view->alert_type = "";
					$this->view->alert_message = '<strong>Warning!</strong> Thiếu thông tin để xác nhận.';
				}
			}
		}
		// process to display view
		$this->view->total_report = $mReport->countReport ();
		$this->view->my_user_id = $this->USER_ID;
		$arrReport = $mReport->listAllReportPaging ();
		if (count ( $arrReport ) != 0) {
			// paging
			$adapter = new Zend_Paginator_Adapter_DbSelect ( $arrReport );
			$paginator = new Zend_Paginator ( $adapter );
			$paginator->setItemCountPerPage ( 100 );
			$paginator->setPageRange ( 3 );
			$currentPage = $this->_request->getParam ( 'page', 1 );
			$paginator->setCurrentPageNumber ( $currentPage );
			$this->view->arrReport = $paginator;
			
			// get comment info
			$arrCmt = null;
			$arrUser = null;
			$arrPhoto = null;
			$arrLoc = null;
			foreach ( $paginator as $report ) {
				$arrCmtInfo = $mCmt->getCommentInfoById ( $report ['comment_id'] );
				$arrCmt [] = $arrCmtInfo;
				$arrUser [] = $mUser->getUserInfoById ( $arrCmtInfo [0] ['user_id'] );
				$arrPhoto [] = $mPhoto->getPhotoInfoById( $arrCmtInfo [0] ['photo_id'] );
				$arrLoc[] = $mLoc->getLocationInfoById($arrCmtInfo [0] ['location_id']);
			}
			$this->view->arrCmt = $arrCmt;
			$this->view->arrUser = $arrUser;
			$this->view->arrPhoto = $arrPhoto;
			$this->view->arrLoc = $arrLoc;
		}
	}
}